<template>
  <el-dialog
    :title="type === 'in' ? '一键入库' : '一键出库'"
    :visible.sync="visible"
    width="500px"
    :before-close="
      () => {
        $emit('close');
      }
    "
  >
    <el-form
      size="small"
      :rules="rules"
      :model="form"
      ref="inventoryForm"
      label-width="90px"
    >
      <el-form-item label="商品类别" prop="id">
        <el-radio-group v-model="form.id">
          <el-radio v-for="item in spec_list" :key="item.id" :label="item.id"
            >{{ item.spec_name }} ({{ item.spec_inventory }})</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="type === 'in' ? '入库数量' : '出库数量'"
        prop="number"
      >
        <el-input :maxlength="6" v-model="form.number"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="message">
        <el-input v-model="form.message" type="textarea" rows="4" resize="none">
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { inventoryIn, inventoryOut, getInventorySpec } from "@/api/good-mng";
export default {
  props: ["visible", "type", "id"],
  data() {
    return {
      spec_list: [],
      form: {
        number: null,
        message: null,
        id: null
      },
      rules: {
        number: [{ required: true, message: "请输入数量", trigger: "blur" }],
        id: [{ required: true, message: "请选择操作规格", trigger: "blur" }]
        // message: [{ required: true, message: "请输入可选值", trigger: "blur" }]
      }
    };
  },
  watch: {
    visible: function(val) {
      if (val) {
        getInventorySpec({ id: this.id }).then(res => {
          if (res.code === 200) {
            this.spec_list = res.data.spec;
          }
        });
      } else {
        this.$refs["inventoryForm"].resetFields();
        this.spec_list = [];
      }
    }
  },
  methods: {
    submit() {
      this.$refs["inventoryForm"].validate(valid => {
        if (valid) {
          let obj = { ...this.form };
          // obj.id = this.id;
          if (this.type === "in") {
            inventoryIn(obj).then(res => {
              if (res.code === 200) {
                this.$message({
                  message: "入库成功",
                  type: "success"
                });
                this.$emit("close");
              }
            });
          } else {
            inventoryOut(obj).then(res => {
              if (res.code === 200) {
                this.$message({
                  message: "出库成功",
                  type: "success"
                });
                this.$emit("close");
              }
            });
          }
        }
      });
    }
  },
  created() {}
};
</script>

<style></style>
